/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cl.mechon.buchef.engine;

import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

/**
 *
 * @author Linkup
 */
public class GameRenderer {
    
    GameLogic my_logic;

    public GameRenderer(GameLogic my_logic) {
        this.my_logic = my_logic;
    }
    
    public void render(GameContainer container, StateBasedGame sb, Graphics g) throws SlickException{
        
        Map<Integer, LinkedList<Entity>> render_order= new HashMap<Integer,  LinkedList<Entity>>();
        
        for(Entity a_entity : my_logic.getRenderable())
        {
            LinkedList<Entity> a_list = render_order.get(a_entity.getLayer());
            
            if(a_list ==null || a_list.isEmpty()){
                a_list= new LinkedList<Entity>();
            }
            
            a_list.add(a_entity);
            render_order.put(a_entity.getLayer(),a_list);
        }
        
        List orderedlayers = new ArrayList(render_order.keySet());
        
        Collections.sort(orderedlayers);
        
        for (Iterator it = orderedlayers.iterator(); it.hasNext();) {
            int i = (int)it.next();
            
            for(Entity a_entity : render_order.get(i)){
                a_entity.render(container, sb, g);
            }
        }
    }
}
